<?php #Campionat virtual - campionat.php
require_once("/includes/config.php");
$page_title = "Inregistrare |";
include_once("/includes/head.php");
?>
<body>
<?php
include_once("/includes/header.php");
?>
<!--body start -->
<div id="body">
<!--left panel start -->
<?php
include_once("/includes/sidea.php");
?>
<!--left panel end -->
<!--right panel start -->
<div id="right">
<h1>Aici te inscrii</h1>
            <hr  color='#CCCCCC' width='500px' align='left' />
        <?php
			if(isset($_POST["submitted"]))
			{
				require_once("/includes/mysql_connect.php");
				//Verifica existenta prenumelui
				if(stripslashes(trim($_POST["first_name"])))
				{
					$first_name = escape_data($_POST["first_name"]);
				}
				else
				{
					$first_name = FALSE;
					echo "<p><font color = 'red'>Prenumele este obligatoriu.</font></p>";
				}
				
				//Verifica existenta numelui de familie.
				if(stripslashes(trim($_POST["last_name"])))
				{
					$last_name = escape_data($_POST["last_name"]);
				}
				else
				{
					$last_name = FALSE;
					echo "<p><font color = 'red'>Numele este obligatoriu.</font></p>";
				}
				
				//Verifica existenta email.
				if(stripslashes(trim($_POST["email"])))
				{
					$email = escape_data($_POST["email"]);
				}
				else
				{
					$email = FALSE;
					echo "<p><font color='red'>Introdu o adresa de e-mail valida.</font></p>";
				}
				
				//Verifica daca este selectat checkboxul
				if(isset($_POST["agreement"]))
				{
					$check = TRUE;
				}
				else
				{
					$check = FALSE;
					echo "<p><font color='red'>Trebuie sa fii de acord cu termenii, regulile si conditiile acestui site.</font></p>";
				}
				
				//Verifica existenta unei parole si o compara cu parola confirmata
				if(stripslashes(trim($_POST["password1"])))
				{
					if($_POST["password1"] = $_POST["password2"])
					{
						$password = escape_data($_POST["password1"]);
					}
					else
					{
						$password = FALSE;
						echo "<p><font color='red'>Parola nu se potriveste cu parola confirmata</font></p>";
					}
				}
				else
				{
					$password = FALSE;
					echo "<p><font color='red'>Insereaza o parola valida.</font></p>";
				}
				if ($first_name && $last_name && $email && $password)
				{//Daca totul este in regula
					//Se asigura ca emailul este disponibil
					$query = "SELECT user_id FROM users WHERE email = '$email'";
					$result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error:" .mysql_error());
					
					if(mysql_num_rows($result) == 0)
					{//Disponibila
						//Adauga utilizatorul
						$query = "INSERT INTO users (email, password, first_name, last_name, registered_on) VALUES ('$email', SHA('$password'), '$first_name', '$last_name', NOW() )";
						$result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error:" . mysql_error());
						
						if(mysql_affected_rows() == 1) 
						{//Daca rularea a decurs fara probleme.
							//Transmite mesajul de posta electronica.
							$body = "Salut! Acum esti inregistrat pe Trivia Crestina \n";
							$body .= "Trivia Crestina";
							mail($_POST["email"], 'Confirmarea inregistrarii', $body);
							//Termina pagina
							echo "<h3>Te-ai inregistrat! Un mesaj de confirmare a fost trimis pe emailul tau.</h3>";
						}
						else
						{//Daca apar probleme
							echo "<p><font color='red'>Scuze. Eroare de sistem. Nu ai putut fi inregistrat.</font></p>";
						}
					}
					else
					{//Adresa de posta electronica nu este disponibila
						echo "<p><font color='red'>Aceasta adresa de email este deja inregistrata</font></p>";
					}
					
					mysql_close();
				}
				}
					?>
        	
            <fieldset>
            <legend><p class="text1">Inregistreaza-te!</p></legend>
            <form action="campionat.php" method="post">
            <table>
            <tr>
            <td><p class="text2">Prenumele: </p></td><td><input type="text" name="first_name" size="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name'];?>"></td>
            </tr>
            <tr>
            <td><p class="text2">Numele: </p></td><td><input type="text" size="20" name="last_name" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name'];?>"></td>
            </tr>
            <tr>
            <td><p class="text2">Email: </p></td><td><input type="text" name="email" size="20" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>"></td>
            </tr>
            <tr>
            <td><p class="text2">Parola: </p></td><td><input type="password" name="password1" size="20"></td><td><small>4-20 caractare</small></td>
            </tr>
            <tr>
            <td><p class="text2">Confirma parola: </p></td><td><input type="password" name="password2" size="20"></td>
            </tr> 
            </table>
            <input type="checkbox" name="agreement" value="milk"><p class="text2">Sunt de acord cu <a href="termeni_conditii_reguli.php" target="_blank">termenii, conditiile si regulile</a> acestui site.</p>
            <input type="submit" name="submit" value="Inscrie-te">
            <input type="hidden" name="submitted" value="TRUE"/>
            </form>
            </fieldset>
            <br><br><br>
            
		</div>
<!--right panel end -->
<br class="spacer" />
</div>
<!--body end -->
<!--footer start -->
<?php
include_once("/includes/footer.php");
?>
<!--footer end -->
</body>
</html>